/*
  Copyright 2007 Anders Båtstrand

      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

 */

package org.dyndns.anderlin.jmplayer.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Entity
@NamedQueries({
@NamedQuery(name = "album_bysearch", query = "select a from Album a where upper(a.title) like :text order by a.title"),
@NamedQuery(name = "album_all", query = "select a from Album a order by a.title")
        })
public class Album extends JmplayerObject {

    /**
     * The title of the album. Ie Marc-André Hamelin live at Wigmore Hall
     */
    @Basic
    @Column(nullable = false)
    private String title;

    /**
     * unique id, get it from where? TODO: Decide
     */
    @Basic
    @Column(nullable = false)
    private String disc_id;

    /**
     * The date of release
     */
    @Temporal(TemporalType.DATE)
    private Date released;

    @OneToMany(cascade = CascadeType.MERGE, mappedBy = "album", fetch = FetchType.EAGER)
    @OrderBy("trackNumber")
    private List<Track> tracks = new ArrayList<Track>();

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDisc_id() {
        return disc_id;
    }

    public void setDisc_id(String disc_id) {
        this.disc_id = disc_id;
    }


    public Date getReleased() {
        return released;
    }

    public void setReleased(Date released) {
        this.released = released;
    }

    public List<Track> getTracks() {
        return tracks;
    }

    public void setTracks(List<Track> tracks) {
        this.tracks = tracks;
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Album album = (Album) o;

        if (disc_id != null ? !disc_id.equals(album.disc_id) : album.disc_id != null) return false;
        if (released != null ? !released.equals(album.released) : album.released != null) return false;
        if (title != null ? !title.equals(album.title) : album.title != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (title != null ? title.hashCode() : 0);
        result = 31 * result + (disc_id != null ? disc_id.hashCode() : 0);
        result = 31 * result + (released != null ? released.hashCode() : 0);
        return result;
    }

    public String toString() {
        if (title != null) {
            return getTitle();
        } else {
            return "new Album";
        }
    }
}
